package com.thinkingstar.iads.datacenter.entity.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import com.thinkingstar.iads.datacenter.entity.DcProjectDesignFormulaVersion;
import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.util.NutMap;

import com.thinkingstar.iads.common.serviceImpl.EntityRepositoryServiceImpl;
import com.thinkingstar.iads.common.utils.SwapUtil;
import com.thinkingstar.iads.datacenter.entity.DcManufacturerEquipment;
import com.thinkingstar.iads.datacenter.entity.DcProjectDesignConstantVersion;
import com.thinkingstar.iads.datacenter.entity.DcProjectDesignContent;

/**
 * @ClassName: DaoDcProjectDesignConstantVersion 
 * @Description:数据中心_常量_常量版本库维护
 * @author 1157372219@qq.com 
 * @date 2018年3月19日   
 */
@IocBean(args = {"refer:dao"})
public class DaoDcProjectDesignConstantVersion extends EntityRepositoryServiceImpl<DcProjectDesignConstantVersion>{

	public DaoDcProjectDesignConstantVersion(Dao dao) {
		super(dao);
	}
	
	/**
	 * 常量版本库查询
	 * @return
	 */
	public QueryResult findDcProjectDesignConstantList(NutMap nutMap){
		List <Map> list = null;
		Pager pager = null;
		String sqlStr ="select dcv.* , ddp.project_design_phase_name , dpc.project_design_content_name , dpm.project_design_major_name "
				+ " from dc_constant_version dcv "
				+ " left join dc_project_design_phase ddp on dcv.project_design_phase_id = ddp.id and ddp.del_flag = '0' "
				+ " left join dc_project_design_content dpc on dcv.project_design_content_id = dpc.id and dpc.del_flag = '0' "
				+ " left join dc_project_design_major dpm on dcv.project_design_major_id = dpm.id and dpm.del_flag = '0' "
				+ " where dcv.del_flag = '0' ";
		if (nutMap.get("constant_table_name")!=null) {
			sqlStr +="and dcv.constant_table_name like @constant_table_name";
		}
		StringBuffer stringBuffer = new StringBuffer(sqlStr
				+ " order by dcv.create_time desc ");

		Sql sql = Sqls.create(stringBuffer.toString());
		if (nutMap.get("constant_table_name")!=null) {
			sql.params().set("constant_table_name","%"+nutMap.get("constant_table_name")+"%");
		}
		if(nutMap.get("page_num") != null && nutMap.get("page_size") != null){
			pager = new Pager(nutMap.getInt("page_num"), nutMap.getInt("page_size"));
			pager.setRecordCount(this.findDcProjectDesignConstantSize());
			sql.setPager(pager);
		}
		sql.setCallback(new SqlCallback() {
			List<Map> list = new LinkedList<>();
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				while (rs.next()){
					Map <String , Object> map = new HashMap<>();
					map.put("id", rs.getString("id"));
					map.put("constant_table_code", rs.getString("constant_table_code"));
					map.put("constant_table_name", rs.getString("constant_table_name"));
					map.put("constant_table_version_number", rs.getString("constant_table_version_number"));
					map.put("project_design_phase_name", rs.getString("project_design_phase_name"));
					map.put("project_design_content_name", rs.getString("project_design_content_name"));
					map.put("project_design_major_name", rs.getString("project_design_major_name"));
					map.put("create_time", rs.getString("create_time"));
					map.put("update_time", rs.getString("update_time"));
					map.put("del_flag", rs.getBoolean("del_flag"));
					list.add(map);
				}
				SwapUtil.clearNull(list);
				return list;
			}
		});
		dao().execute(sql);
		list = sql.getList(Map.class);
		return new QueryResult(list,pager);
	}
	
	/**
	 * 查询总数量
	 * @param
	 * @return
	 */
	public int findDcProjectDesignConstantSize(){
		StringBuffer stringBuffer = new StringBuffer("select count(dcv.id) size_num "
				+ " from dc_constant_version dcv "
				+ " left join dc_project_design_phase ddp on dcv.project_design_phase_id = ddp.id and ddp.del_flag = '0' "
				+ " left join dc_project_design_content dpc on dcv.project_design_content_id = dpc.id and dpc.del_flag = '0' "
				+ " left join dc_project_design_major dpm on dcv.project_design_major_id = dpm.id and dpm.del_flag = '0' "
				+ " where dcv.del_flag = '0' ");
		Sql sql = Sqls.create(stringBuffer.toString());
		sql.setCallback(new SqlCallback() {
			int count = 0;
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				while (rs.next())
				count = rs.getInt("size_num");
				return count;
			}
		});
		dao().execute(sql);
		return sql.getInt();
	}
	
	
	//查询版本号
	public DcProjectDesignConstantVersion findConstantVersionByCondition(DcProjectDesignConstantVersion dcProjectDesignConstantVersion){
		Condition cnd = Cnd.where("del_flag", "=", '0').and("project_design_phase_id", "=", dcProjectDesignConstantVersion.getProject_design_phase_id()).and("project_design_content_id", "=", dcProjectDesignConstantVersion.getProject_design_content_id())
				.and("project_design_major_id", "=", dcProjectDesignConstantVersion.getProject_design_major_id()).and("constant_table_code", "=", dcProjectDesignConstantVersion.getConstant_table_code())
				.and("constant_table_name", "=", dcProjectDesignConstantVersion.getConstant_table_name()).limit(1, 1).desc("create_time");
		DcProjectDesignConstantVersion obj = fetch(cnd);
		if(obj != null){
			return obj;
		}
		return null;
	}

	/**
	 * 根据条件(阶段id,专业id,设计内容id)查询符合条件的 常量集列表
	 * @param nutMap
	 * @return
	 */
	public List<DcProjectDesignConstantVersion> findConstantVersionList(NutMap nutMap){
		List <Map> list = null;
		String find_sql = " select * from dc_constant_version where project_design_content_id=@content_id and project_design_major_id=@major_id and project_design_phase_id=@phase_id " +
				"and del_flag='0' order by constant_table_version_number desc ";
		Sql sql = Sqls.create(find_sql);
		sql.params().set("content_id", nutMap.get("content_id"));
		sql.params().set("major_id", nutMap.get("major_id"));
		sql.params().set("phase_id", nutMap.get("phase_id"));
		sql.setCallback(Sqls.callback.entities());
		sql.setEntity(dao().getEntity(DcProjectDesignConstantVersion.class));
		dao().execute(sql);
		return sql.getList(DcProjectDesignConstantVersion.class);
	}
}
